Page({
  onFeeInput(e) {
    let value = e.detail.value; 
    // 过滤非法字符（仅保留数字和一个小数点）
    value = value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace(/^\./, '');
    this.setData({
      feeValue: value,
      amount: value ? Number(value) : 0
    });
  },
  data: {
    // 物品重量
    weightValue: "25公斤以内",
    weightOptions: ["25公斤以内", "26-30公斤", "31-40公斤", "41-50公斤"],

    // 取货地址
    addressInput: "", 
    tempFilePaths: [], 

    // 送达时间
    time: "尽快送达", 
    showOtherTimeModal: false, 
    otherTimeList: [
      "8:00-10:00",
      "10:00-12:00",
      "12:00-14:00",
      "14:00-16:00",
      "16:00-18:00",
      "18:00-20:00",
      "20:00-22:00"
    ], 
    selectedOtherTimeIndex: 0, 
    isOtherSelected: false,
    // 订单金额
    feeValue: "", 
    amount: 0 ,
    imagePath:''//用于存储图片临时路径
  },
  // 选择物品重量
  chooseWeight() {
    wx.showActionSheet({
      itemList: this.data.weightOptions,
      success: (res) => {
        this.setData({
          weightValue: this.data.weightOptions[res.tapIndex]
        });
      }
    });
  },

  // 上传取货地址图片
  chooseImage() {
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // 注意：wx.chooseMedia 返回的是 res.tempFiles[0].tempFilePath
        const tempFilePath = res.tempFiles[0].tempFilePath;
        this.setData({
          imagePath: tempFilePath
        });
      }
    });
  },

  // 输入取货地址文字
  onAddressInput(e) {
    this.setData({
      addressInput: e.detail.value
    });
  },

  // 选择常规送达时间
  chooseTime(e) {
    this.setData({
      time: e.currentTarget.dataset.time,
      isOtherSelected: false // 选常规时间时，重置“其他”选中标记
    });
  },

  // 打开“其他”时间弹窗
  openOtherTimeModal() {
    this.setData({
      showOtherTimeModal: true
    });
  },

  // 切换“其他”时间选项
  onOtherTimeChange(e) {
    this.setData({
      selectedOtherTimeIndex: e.detail.value
    });
  },

  // 确认“其他”时间
  confirmOtherTime() {
    const selectedTime = this.data.otherTimeList[this.data.selectedOtherTimeIndex];
    this.setData({
      time: selectedTime,
      showOtherTimeModal: false,
      isOtherSelected: true // 选中后标记为 true
    });
  },

  // 取消“其他”时间
  closeOtherTimeModal() {
    this.setData({
      showOtherTimeModal: false,
      isOtherSelected: false // 取消后标记为 false
    });
  },

  // 发布订单（示例，需对接后端可扩展）
  releaseOrder() {
    const { 
      weightValue, 
      addressInput, 
      time, 
      amount, 
      tempFilePaths 
    } = this.data;
    console.log("发布订单数据：", {
      weightValue,
      addressInput,
      time,
      amount,
      tempFilePaths
    });
    wx.showToast({
      title: "订单发布中...",
      icon: "loading",
      duration: 1500
    });
  }
});